package com.pth.dao.impl;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.pth.dao.ExamDAO;
import com.pth.model.Exam;
import com.pth.utils.ExamState;

public class ExamDAOImpl implements ExamDAO {

	private HibernateTemplate hibernateTemplate;
	
	public void save(Exam exam) {

		hibernateTemplate.save(exam);
		
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public Exam getExamByeid(int eid) {

		Exam  exam=hibernateTemplate.get(Exam.class, eid);
		
		return exam;
	}

	public void delete(Exam exam) {

		hibernateTemplate.delete(exam);
	}

	public void update(Exam exam) {

		hibernateTemplate.update(exam);
		
	}

	@SuppressWarnings("unchecked")
	public Exam getLastExam() {

		Exam exam=null;
		List<Exam> exams=hibernateTemplate.find("from Exam");
		System.out.println("size="+exams.size());
		Iterator<Exam> eIterator=exams.iterator();
		
		while (eIterator.hasNext()) {

			exam=eIterator.next();
		}

		return exam;
		
	}

	@SuppressWarnings("unchecked")
	public List<Exam> getExamNoOff(final int pagesize,final int offset) {

		final String hql="from Exam e where e.state="+ExamState.EXAM_ON;

		List<Exam> list=hibernateTemplate.executeFind(new HibernateCallback() {

			public Object doInHibernate(Session arg0) throws HibernateException,
					SQLException {

				return arg0.createQuery(hql).setFirstResult(offset).setMaxResults(pagesize).list();
				
			}
		});
		
		return list;
		
	}
	
	
	public int getnumStateON() {

		final String hql="from Exam e where e.state="+ExamState.EXAM_ON;
		
		return hibernateTemplate.find(hql).size();
	
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Exam> getExamONforTask() {

		final String hql="from Exam e where e.state="+ExamState.EXAM_ON;
		
		return hibernateTemplate.find(hql);
	
	}

	@SuppressWarnings("unchecked")
	public List<Exam> getallExams() {
		
		final String hql="from Exam e order by e.examtime desc";
		
		return hibernateTemplate.find(hql);
		
	}
	
	
	
	public int getAllNum() {

		return hibernateTemplate.find("from Exam").size();
	
	}

	@SuppressWarnings("unchecked")
	public List<Exam> getExamBMON(final int pagesize,final int offset) {


		final String hql="from Exam e where e.bmstate="+ExamState.EXAM_BM_ON;

		List<Exam> list=hibernateTemplate.executeFind(new HibernateCallback() {

			public Object doInHibernate(Session arg0) throws HibernateException,
					SQLException {

				return arg0.createQuery(hql).setFirstResult(offset).setMaxResults(pagesize).list();
				
			}
		});
		
		return list;
		
	}

	@SuppressWarnings("unchecked")
	public List<Exam> getExamBMONforTask() {
		
		final String hql="from Exam e where e.bmstate="+ExamState.EXAM_BM_ON;
		
		return hibernateTemplate.find(hql);
	}

	public int getnumStateBmON() {
		
		final String hql="from Exam e where e.bmstate="+ExamState.EXAM_BM_ON;
		
		return hibernateTemplate.find(hql).size();
	}

	@SuppressWarnings("unchecked")
	public Exam getExamByExamtime(String examtime) {
			
		String hql="from Exam e where e.examtime='"+examtime+"'";
		
		List<Exam> exams=hibernateTemplate.find(hql);
		
		if(exams.size()==0){
			
			return null;
		}
		else{
		Iterator<Exam> iterator=exams.iterator();
		
		Exam exam=new Exam();
		
		while(iterator.hasNext()){
			
			exam=iterator.next();
			
		}
		
		return exam;
	}
		}

}
